using System.Collections.Generic;
using System.Text;

namespace NFreeMarker.Core
{
    /**
     * Encapsulates an array of <tt>TemplateElement</tt> objects. 
     */

    internal sealed class MixedContent : TemplateElement
    {
        internal MixedContent()
        {
            NestedElements = new List<TemplateElement>();
        }

        internal void AddElement(TemplateElement element)
        {
            NestedElements.Add(element);
        }

        internal override TemplateElement PostParseCleanup(bool stripWhitespace)
        {
            base.PostParseCleanup(stripWhitespace);
            return NestedElements.Count == 1 ? NestedElements[0] : this;
        }

        /**
         * Processes the contents of the internal <tt>TemplateElement</tt> list,
         * and outputs the resulting text.
         */

        internal override void Accept(Environment env)
        {
            foreach (TemplateElement element in NestedElements)
            {
                env.Visit(element);
            }
        }

        public override string GetCanonicalForm()
        {
            var buf = new StringBuilder();
            foreach (TemplateElement element in NestedElements)
            {
                buf.Append(element.GetCanonicalForm());
            }
            return buf.ToString();
        }

        public override string GetDescription()
        {
            return Parent == null ? "root element" : "content"; // MixedContent is uninteresting in a stack trace.
        }

        internal override bool IsIgnorable()
        {
            return NestedElements == null || NestedElements.Count == 0;
        }
    }
}